package com.handsome.service.impl;

import com.handsome.dao.HsMemberMapper;
import com.handsome.dao.HsMenuMapper;
import com.handsome.entity.HsMenu;
import com.handsome.service.AuthService;
import com.handsome.utils.MenuTreeUtil;
import com.handsome.utils.Result;
import com.handsome.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 权限
 *
 * @author Jiangda
 */
@Service
public class AuthServiceImpl implements AuthService {


    @Autowired
    private HsMemberMapper hsMemberMapper;

    @Autowired
    private HsMenuMapper hsMenuMapper;

    @Override
    public List<HsMenu> getMenus(Boolean isTree) {
        List<HsMenu> result = null;
        result = hsMenuMapper.selectAll();
        if (isTree) {
            result = MenuTreeUtil.generateTree(result, 0);
        }
        return result;
    }

    @Override
    public List<HsMenu> getMenusByAuth(Boolean isTree) {
        return null;
    }

    @Override
    public Result actionMenu(HsMenu menu) {
        menu.setIsOpen(0);
        menu.setCreateTime(new Date());
        menu.setEditTime(new Date());
        menu.setIsDisabled(0);
        // 修改
        if (menu.getId() != null) {
            if (hsMenuMapper.updateByPrimaryKey(menu) > 0) {
                return ResultGenerator.genSuccessResult();
            }
        }
        // 添加
        else {
            if (hsMenuMapper.insert(menu) > 0) {
                return ResultGenerator.genSuccessResult();
            }
        }

        return ResultGenerator.genFailResult("操作失败,请稍后再试");
    }
}
